/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.pay.bean;

import com.yunhoo.pay.model.Expense;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.FacesUtils;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.NavigationTool;
import com.yunhoo.sys.util.common.Null;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "payBillViewBean")
@SessionScoped
public class PayBillViewBean implements Serializable {

    private List<Expense> viewList = new ArrayList<Expense>();//翻页数据集
    private DataModel viewListModel = new ListDataModel();
    private DataModel loanModel = new ListDataModel();//3
    private DataModel expenseModel = new ListDataModel();//0
    private DataModel travelCostModel = new ListDataModel();//1
    private DataModel abroadGroupModel = new ListDataModel();//6
    private DataModel payApplicationModel = new ListDataModel();//4
    private DataModel costProofModel = new ListDataModel();//5

    public void queryCheckViewList() {

        LoanBillBean loanBillBean = (LoanBillBean) FacesUtils.getManagedBean("loanbill");
        ExpenseBean expenseBean = (ExpenseBean) FacesUtils.getManagedBean("expense");
        TravelCostBean travelCostBean = (TravelCostBean) FacesUtils.getManagedBean("tripaccount");
        AbroadGroupCheckBean abroadGroupBean = (AbroadGroupCheckBean) FacesUtils.getManagedBean("abroadGroupCheckBean");
        PayApplicationCheckBean payApplicationBean = (PayApplicationCheckBean) FacesUtils.getManagedBean("payAppCheck");
        CostProofCheckBean costProofBean = (CostProofCheckBean) FacesUtils.getManagedBean("proofCheck");
        loanBillBean.initCheckViewList();
        expenseBean.initCheckViewList();
        travelCostBean.initCheckViewList();
        abroadGroupBean.initCheckViewList();
        payApplicationBean.initCheckViewList();
        costProofBean.initCheckViewList();
    }

    public void queryCheckViewAllList() {
        Map map = new HashMap();
        map.put("voustatus", "0");
        map.put("orgCode", curOrgCode().trim());
        viewList = IbatisTemplate.queryForList("commOper.queryViewALlList", map);
        viewListModel.setWrappedData(viewList);
        if (!Null.check(viewList)) {
            formatList(viewList);
        }
    }

    public void selectedFromView() {
        Expense expense = (Expense) viewListModel.getRowData();
        //报销单
        if (expense.getVoutype().trim().equals("3")) {
            LoanBillBean loanBillBean = (LoanBillBean) FacesUtils.getManagedBean("loanbill");
            loanBillBean.showFromCheckView(expense, loanModel);
        } else if (expense.getVoutype().trim().equals("0")) {
            ExpenseBean expenseBean = (ExpenseBean) FacesUtils.getManagedBean("expense");
            expenseBean.showFromCheckView(expense, expenseModel);
        } else if (expense.getVoutype().trim().equals("1")) {
            TravelCostBean travelCostBean = (TravelCostBean) FacesUtils.getManagedBean("tripaccount");
            travelCostBean.showFromCheckView(expense, travelCostModel);
        } else if (expense.getVoutype().trim().equals("6")) {
            AbroadGroupCheckBean abroadGroupBean = (AbroadGroupCheckBean) FacesUtils.getManagedBean("abroadGroupCheckBean");
            abroadGroupBean.showFromCheckView(expense, abroadGroupModel);
        } else if (expense.getVoutype().trim().equals("4")) {
            PayApplicationCheckBean payApplicationBean = (PayApplicationCheckBean) FacesUtils.getManagedBean("payAppCheck");
            payApplicationBean.showFromCheckView(expense, payApplicationModel);
        } else if (expense.getVoutype().trim().equals("5")) {
            CostProofCheckBean costProofBean = (CostProofCheckBean) FacesUtils.getManagedBean("proofCheck");
            costProofBean.showFromCheckView(expense, costProofModel);
        }
    }

    public void formatList(List<Expense> viewList) {
        List<Expense> loanList = new ArrayList<Expense>();
        List<Expense> expenseList = new ArrayList<Expense>();
        List<Expense> travelCostList = new ArrayList<Expense>();
        List<Expense> abroadGroupList = new ArrayList<Expense>();
        List<Expense> payAppList = new ArrayList<Expense>();
        List<Expense> costProofList = new ArrayList<Expense>();
        for (Expense v : viewList) {
            if (v.getVoutype().trim().equals("3")) {
                loanList.add(v);
                loanModel.setWrappedData(loanList);
            } else if (v.getVoutype().trim().equals("0")) {
                expenseList.add(v);
                expenseModel.setWrappedData(expenseList);
            } else if (v.getVoutype().trim().equals("1")) {
                travelCostList.add(v);
                travelCostModel.setWrappedData(travelCostList);
            } else if (v.getVoutype().trim().equals("6")) {
                abroadGroupList.add(v);
                abroadGroupModel.setWrappedData(abroadGroupList);
            } else if (v.getVoutype().trim().equals("4")) {
                payAppList.add(v);
                payApplicationModel.setWrappedData(payAppList);
            } else if (v.getVoutype().trim().equals("5")) {
                costProofList.add(v);
                costProofModel.setWrappedData(costProofList);
            }
        }
    }
    /* 查看待审批单据 */

    public void showChecks() {
        NavigationTool.getSelectedUrl("/page/pay/check/view/checkView.xhtml");
    }

    /*批量审批*/
    public void batchCheck() {
        if (Null.check(viewList)) return;
        for (Expense expense : viewList) {
            if (expense.isSelected()) {
                System.out.println("numberStatus : " + expense.getNumberStatus());
                if (expense.getVoutype().trim().equals("3")) {
                    LoanBillBean loanBillBean = (LoanBillBean) FacesUtils.getManagedBean("loanbill");
                    loanBillBean.passWithParam(expense);
                } else if (expense.getVoutype().trim().equals("0")) {
                    ExpenseBean expenseBean = (ExpenseBean) FacesUtils.getManagedBean("expense");
                    expenseBean.passWithParam(expense);
                } else if (expense.getVoutype().trim().equals("1")) {
                    TravelCostBean travelCostBean = (TravelCostBean) FacesUtils.getManagedBean("tripaccount");
                    travelCostBean.passWithParam(expense);
                } else if (expense.getVoutype().trim().equals("6")) {
                    AbroadGroupCheckBean abroadGroupBean = (AbroadGroupCheckBean) FacesUtils.getManagedBean("abroadGroupCheckBean");
                    abroadGroupBean.passWithParam(expense);
                } else if (expense.getVoutype().trim().equals("4")) {
                    PayApplicationCheckBean payApplicationBean = (PayApplicationCheckBean) FacesUtils.getManagedBean("payAppCheck");
                    payApplicationBean.passWithParam(expense);
                } else if (expense.getVoutype().trim().equals("5")) {
                    CostProofCheckBean costProofBean = (CostProofCheckBean) FacesUtils.getManagedBean("proofCheck");
                    costProofBean.passWithParam(expense);
                }
            }
        }
        queryCheckViewAllList();
    }

    public List<Expense> getViewList() {
        return viewList;
    }

    public void setViewList(List<Expense> viewList) {
        this.viewList = viewList;
    }

    public DataModel<Expense> getViewListModel() {
        return viewListModel;
    }

    public void setViewListModel(DataModel<Expense> viewListModel) {
        this.viewListModel = viewListModel;
    }

    private String curOrgCode() {
        return LoginParam.getOrg().getCode();
    }

    public DataModel getAbroadGroupModel() {
        return abroadGroupModel;
    }

    public void setAbroadGroupModel(DataModel abroadGroupModel) {
        this.abroadGroupModel = abroadGroupModel;
    }

    public DataModel getCostProofModel() {
        return costProofModel;
    }

    public void setCostProofModel(DataModel costProofModel) {
        this.costProofModel = costProofModel;
    }

    public DataModel getExpenseModel() {
        return expenseModel;
    }

    public void setExpenseModel(DataModel expenseModel) {
        this.expenseModel = expenseModel;
    }

    public DataModel getLoanModel() {
        return loanModel;
    }

    public void setLoanModel(DataModel loanModel) {
        this.loanModel = loanModel;
    }

    public DataModel getPayApplicationModel() {
        return payApplicationModel;
    }

    public void setPayApplicationModel(DataModel payApplicationModel) {
        this.payApplicationModel = payApplicationModel;
    }

    public DataModel getTravelCostModel() {
        return travelCostModel;
    }

    public void setTravelCostModel(DataModel travelCostModel) {
        this.travelCostModel = travelCostModel;
    }
}
